#################################################################################
# Replication file for: "Evidence of Conflict Extension in Partisans' Evaluations 
#                        of People and Inanimate Objects."                      #
#                                                                               #
#                                                                               #
# Kevin K. Banda                                                                #
# Department of Political Science, Texas Tech University                        #
# kevin.banda@ttu.edu                                                           #
#                                                                               #
# Thomas M. Carsey                                                              #
# Department of Political Science, University of North Carolina at Chapel Hill  #
# carsey@unc.edu                                                                #
#                                                                               #
# Serge Severenchuk                                                             #
# Department of Political Science, University of North Carolina at Chapel Hill  #
# sserge@unc.edu                                                                #
#                                                                               #
# Last update: May 29, 2019                                                     #
# R verison used: 3.5.1  
#
################
## Contents in this script are in the following order (labeled as they appear in the manuscript and the Appendix)
## 1) Table 1A Appendix
## 2) Table 1 part B
## 3) Figure 1 Main results for Partisans (including leaners)
## 4) Table 1 part A
## 5) Figure A1 Appendix: results for partisans only (dropping leaners and pures) 
## 6) Figure 2 Results for pure independents
## 7) Figure A2 Appendix: results for all independents (leaners and pures in one category)
## 8) Figure A3 Appendix: results for leaners only 
## 9) Table 2: Testing for effect sizes

# CLEARING OUT ALL OBJECTS THAT MIGHT BE IN THE WORK SPACE FOR R. 
rm(list=ls(all=TRUE))

setwd("") # Set the working directory to whatever you're using.

# Load required packages
library(ggplot2) #Version 3.00

#package to create combined figures 
library(gridExtra) #Version 2.3

#package for ddply function
library(plyr) #Version 1.8.4
data1<-read.csv("replicationdata_partisanship.csv")




## Table A1 in the Appendix
## Summary stats 
round(prop.table(table(data1$pid7)),2)

round(prop.table(table(data1$ideology)),2)

round(prop.table(table(data1$sex)),2)

round(prop.table(table(data1$race)),3)

round(prop.table(table(data1$education)),3)

round(prop.table(table(data1$income)),2)
## End Table A1 in the Appendix



## Begin Table 1 part B.
## Pure Indepenents
## School Experiment
cont<-table(data1$tc1t==1 & data1$pid7==4) # control
cont
prop.table(table(data1$tc1t==1 & data1$pid7==4))

dem<-table(data1$tc1t==2 & data1$pid7==4) # dem cue
dem
prop.table(table(data1$tc1t==2 & data1$pid7==4))

rep<-table(data1$tc1t==3 & data1$pid7==4) # rep cue
rep
prop.table(table(data1$tc1t==3 & data1$pid7==4))


## Co-worker experiment
cont<-table(data1$tc2t==1 & data1$pid7==4) # control
cont
prop.table(table(data1$tc2t==1 & data1$pid7==4))

dem<-table(data1$tc2t==2 & data1$pid7==4) # dem cue
dem
prop.table(table(data1$tc2t==2 & data1$pid7==4))

rep<-table(data1$tc2t==3 & data1$pid7==4) # rep cue
rep
prop.table(table(data1$tc2t==3 & data1$pid7==4))

## New product experiment
cont<-table(data1$tc3t==1 & data1$pid7==4) # control
cont
prop.table(table(data1$tc3t==1 & data1$pid7==4))

dem<-table(data1$tc3t==2 & data1$pid7==4) # dem cue
dem
prop.table(table(data1$tc3t==2 & data1$pid7==4))

rep<-table(data1$tc3t==3 & data1$pid7==4) # rep cue
rep
prop.table(table(data1$tc3t==3 & data1$pid7==4))

## Vacation resort experiment
cont<-table(data1$tc4t==1 & data1$pid7==4) # control
cont
prop.table(table(data1$tc4t==1 & data1$pid7==4))

dem<-table(data1$tc4t==2 & data1$pid7==4) # dem cue
dem
prop.table(table(data1$tc4t==2 & data1$pid7==4))

rep<-table(data1$tc4t==3 & data1$pid7==4) # rep cue
rep
prop.table(table(data1$tc4t==3 & data1$pid7==4))
## End Table 1 part B




data1<-subset(data1, subset=(pid7!=4))## dropping pure independents
## This is necessary so that we are comparing apples to apples.

###
# School Experiment
###
          
# treatment conditions including independents in partisan categories
data1$school_treat_ind[data1$tc1t==1]<-"control" 
#this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$school_treat_ind[data1$tc1t==2 & data1$pid3==1 | data1$tc1t==3 & data1$pid3==3]<-"fellow"
data1$school_treat_ind[data1$tc1t==2 & data1$pid3==3 | data1$tc1t==3 & data1$pid3==1]<-"oppon"

# For t.tests for school experiment
#Likelihood of selecting school by condition
like_schol_control<-data1$tc1lik_st[data1$school_treat_ind=="control"]
like_schol_fellow<-data1$tc1lik_st[data1$school_treat_ind=="fellow"]
like_schol_opponent<-data1$tc1lik_st[data1$school_treat_ind=="oppon"]

#Likelihood of selecting school by condition
comfort_schol_control<-data1$tc1comf2_st[data1$school_treat_ind=="control"]
comfort_schol_fellow<-data1$tc1comf2_st[data1$school_treat_ind=="fellow"]
comfort_schol_opponent<-data1$tc1comf2_st[data1$school_treat_ind=="oppon"]

#########
## The two t-tests for likelihood of selecting school
#control v. co-partisan
ts1<-t.test(like_schol_fellow,like_schol_control)
round(ts1$estimate[1]-ts1$estimate[2],3) # for table in the appendix
round(ts1$conf.int,3)
#control v. partisan opponent
ts2<-t.test(like_schol_opponent,like_schol_control)
round(ts2$estimate[1]-ts2$estimate[2],3) # for table in the appendix
round(ts2$conf.int,3)
## The two t-tests for comfort level with school
#control v. co-partisan
ts3<-t.test(comfort_schol_fellow,comfort_schol_control)
round(ts3$estimate[1]-ts3$estimate[2],3) # for table in the appendix
round(ts3$conf.int,3)
#control v. partisan opponent
ts4<-t.test(comfort_schol_opponent,comfort_schol_control)
round(ts4$estimate[1]-ts4$estimate[2],3) # for table in the appendix
round(ts4$conf.int,3)


#All the info from this t-test for ggplot2
school_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means1   


school_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means2



##############
df <- rbind(school_means1, school_means2)
df$model <- rep(c('Likelihood of Selection', 'Comfort Level'), c(nrow(school_means1), nrow(school_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1a
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.16,.1)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="School Experiment")+
theme(plot.title = element_text(family = 'Helvetica', #plot title options
                              color = 'black', 
                              face = 'bold', 
                              size = 14, 
                              hjust = +.5))


#########################
# End school experiment
#########################


###
# Potential Co-worker Sally Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$coworker_treat_ind[data1$tc2t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid3==1 | data1$tc2t==3 & data1$pid3==3]<-"fellow"
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid3==3 | data1$tc2t==3 & data1$pid3==1]<-"oppon"

# For t.tests for Co-Worker Experiment
#Likelihood of selecting a co-worker
likehire_coworker_control<-data1$tc2hire2_st[data1$coworker_treat_ind=="control"]
likehire_coworker_fellow<-data1$tc2hire2_st[data1$coworker_treat_ind=="fellow"]
likehire_coworker_opponent<-data1$tc2hire2_st[data1$coworker_treat_ind=="oppon"]

#Level of enjoyment enjoyment working with a co-worker
enjoy_coworker_control<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="control"]
enjoy_coworker_fellow<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="fellow"]
enjoy_coworker_opponent<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="oppon"]

#########
## The two t-tests for likelihood of selecting a co-worker
#control v. co-partisan
ts1<-t.test(likehire_coworker_fellow,likehire_coworker_control)
round(ts1$estimate[1]-ts1$estimate[2],3) # for table in the appendix
round(ts1$conf.int,3)
#control v. partisan opponent
ts2<-t.test(likehire_coworker_opponent,likehire_coworker_control)
round(ts2$estimate[1]-ts2$estimate[2],3) # for table in the appendix
round(ts2$conf.int,3)
## The two t-tests for level of enjoyment working with a co-worker
#control v. co-partisan
ts3<-t.test(enjoy_coworker_fellow,enjoy_coworker_control)
round(ts3$estimate[1]-ts3$estimate[2],3) # for table in the appendix
round(ts3$conf.int,3)
#control v. partisan opponent
ts4<-t.test(enjoy_coworker_opponent,enjoy_coworker_control)
round(ts4$estimate[1]-ts4$estimate[2],3) # for table in the appendix
round(ts4$conf.int,3)


#All the info from this t-test for ggplot2
coworker_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means1

#For now just replicates what's above
coworker_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means2



##############
df <- rbind(coworker_means1, coworker_means2)
df$model <- rep(c('Likelihood of Selection', 'Level of Enjoyment'), c(nrow(coworker_means1), nrow(coworker_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1b
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.16,.1)+
 coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="Co-worker Experiment")+
theme(plot.title = element_text(family = 'Helvetica', #plot title options
                              color = 'black', 
                              face = 'bold', 
                              size = 14, 
                              hjust = +.5))


#########################
# End of Co-worker Experient
#########################


###
# New Product Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$newproduct_treat_ind[data1$tc3t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid3==1 | data1$tc3t==3 & data1$pid3==3]<-"fellow"
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid3==3 | data1$tc3t==3 & data1$pid3==1]<-"oppon"

# For t.tests for New Soda Product
#Product interest
prodinter_control<-data1$tc3int2_st[data1$newproduct_treat_ind=="control"]
prodinter_fellow<-data1$tc3int2_st[data1$newproduct_treat_ind=="fellow"]
prodinter_opponent<-data1$tc3int2_st[data1$newproduct_treat_ind=="oppon"]

#Level of enjoyment
prodsucc_control<-data1$tc3suc2_st[data1$newproduct_treat_ind=="control"]
prodsucc_fellow<-data1$tc3suc2_st[data1$newproduct_treat_ind=="fellow"]
prodsucc_opponent<-data1$tc3suc2_st[data1$newproduct_treat_ind=="oppon"]

#########
## The two t-tests for product interest
#control v. co-partisan
ts1<-t.test(prodinter_fellow,prodinter_control)
round(ts1$estimate[1]-ts1$estimate[2],3) # for table in the appendix
round(ts1$conf.int,3)
#control v. partisan opponent
ts2<-t.test(prodinter_opponent,prodinter_control)
round(ts2$estimate[1]-ts2$estimate[2],3) # for table in the appendix
round(ts2$conf.int,3)
## The two t-tests for product success
#control v. co-partisan
ts3<-t.test(prodsucc_fellow,prodsucc_control)
round(ts3$estimate[1]-ts3$estimate[2],3) # for table in the appendix
round(ts3$conf.int,3)
#control v. partisan opponent
ts4<-t.test(prodsucc_opponent,prodsucc_control)
round(ts4$estimate[1]-ts4$estimate[2],3) # for table in the appendix
round(ts4$conf.int,3)


#All the info from this t-test for ggplot2
product_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means1   


#For now just replicates what's above
product_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means2



##############
df <- rbind(product_means1, product_means2)
df$model <- rep(c('Product Interest', 'Product Success'), c(nrow(product_means1), nrow(product_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1c
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.16,.1)+
 coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="New Soda Product Experiment")+
theme(plot.title = element_text(family = 'Helvetica', #plot title options
                              color = 'black', 
                              face = 'bold', 
                              size = 14, 
                              hjust = +.5))


#########################
# End New Product Experient
#########################


###
# Vacation Resort Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$vacation_treat_ind[data1$tc4t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==1 | data1$tc4t==3 & data1$pid3==3]<-"fellow"
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==3 | data1$tc4t==3 & data1$pid3==1]<-"oppon"

# For t.tests vacation resort experiment
#Vacation Resort Interest
vacation_control<-data1$tc4int2_st[data1$vacation_treat_ind=="control"]
vacation_fellow<-data1$tc4int2_st[data1$vacation_treat_ind=="fellow"]
vacation_opponent<-data1$tc4int2_st[data1$vacation_treat_ind=="oppon"]

#########
## T-test for vacation resort interst
#control v. co-partisan
ts1<-t.test(vacation_fellow,vacation_control)
round(ts1$estimate[1]-ts1$estimate[2],3) # for table in the appendix
round(ts1$conf.int,3)
#control v. partisan opponent
ts2<-t.test(vacation_opponent,vacation_control)
round(ts2$estimate[1]-ts2$estimate[2],3) # for table in the appendix
round(ts2$conf.int,3)

#All the info from this t-test for ggplot2
vacation_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
            .Names = c("param", "low2.5", "est", "up2.5"), 
            row.names = 1:2, class = c("data.frame", "mplus.params"))
vacation_means1   

##############
df <- vacation_means1
df$model <- rep('Vacation Interest', nrow(vacation_means1))
df$param_f <- factor(df$param, rev(c('Co-Partisans', "Opposing Partisans")))

## Subfigure 1d
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.16,.1)+
 coord_flip() +
facet_grid(~model)+ #
  labs(x="",y="", title="Vacation Resort")+
theme(plot.title = element_text(family = 'Helvetica', #plot title options
                              color = 'black', 
                              face = 'bold', 
                              size = 14, 
                              hjust = +.5))


#########################
# End Vacation Resort Experient
#########################


##################################
## Table 1 part A: Expusure of partisans and (pure) independents to different cues
######## 
## School Experiment
cont<-table(data1$school_treat_ind=="control"  & data1$pid7!=4)  ## excluding pure independents
cont
prop.table(table(data1$school_treat_ind=="control"  & data1$pid7!=4))

co_part<-table(data1$school_treat_ind=="fellow") ## partisans exposed to co-partisan cue 
co_part
prop.table(table(data1$school_treat_ind=="fellow"))

opp_part<-table(data1$school_treat_ind=="oppon") ## partisans exposed to oppoent's cue
opp_part
prop.table(table(data1$school_treat_ind=="oppon"))


## Co-worker experiment
cont<-table(data1$coworker_treat_ind=="control"  & data1$pid7!=4)  
cont
prop.table(table(data1$coworker_treat_ind=="control"  & data1$pid7!=4))

co_part<-table(data1$coworker_treat_ind=="fellow") 
co_part
prop.table(table(data1$coworker_treat_ind=="fellow"))

opp_part<-table(data1$coworker_treat_ind=="oppon") 
opp_part
prop.table(table(data1$coworker_treat_ind=="oppon"))


## New product experiment
cont<-table(data1$newproduct_treat_ind=="control"  & data1$pid7!=4)  
cont
prop.table(table(data1$newproduct_treat_ind=="control"  & data1$pid7!=4))

co_part<-table(data1$newproduct_treat_ind=="fellow") 
co_part
prop.table(table(data1$newproduct_treat_ind=="fellow"))

opp_part<-table(data1$newproduct_treat_ind=="oppon") 
opp_part
prop.table(table(data1$newproduct_treat_ind=="oppon"))


## Vacation resort experiment
cont<-table(data1$vacation_treat_ind=="control"  & data1$pid7!=4)  
cont
prop.table(table(data1$vacation_treat_ind=="control"  & data1$pid7!=4))

co_part<-table(data1$vacation_treat_ind=="fellow") 
co_part
prop.table(table(data1$vacation_treat_ind=="fellow"))

opp_part<-table(data1$vacation_treat_ind=="oppon") 
opp_part
prop.table(table(data1$vacation_treat_ind=="oppon"))
## End Table 1 Part A.











##########################
## Figure A1 in the Appendix
## Main results for partisans only (dropping leaners and pures)
################
# CLEARING OUT ALL OBJECTS THAT MIGHT BE IN THE WORK SPACE FOR R. 
rm(list=ls(all=TRUE))

data1<-read.csv("replicationdata_partisanship.csv")
## dropping responses for leaners and pure independents
data1<-subset(data1, subset=(pid7!=3 | pid7!=4 |pid7!=5 ))
              
###
# School Experiment
###
# treatment conditions including independents in partisan categories
data1$school_treat_ind[data1$tc1t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$school_treat_ind[data1$tc1t==2 & data1$pid3==1 | data1$tc1t==3 & data1$pid3==3]<-"fellow"
data1$school_treat_ind[data1$tc1t==2 & data1$pid3==3 | data1$tc1t==3 & data1$pid3==1]<-"oppon"
              
# For t.tests for school experiment
#Likelihood of selecting school by condition
like_schol_control<-data1$tc1lik_st[data1$school_treat_ind=="control"]
like_schol_fellow<-data1$tc1lik_st[data1$school_treat_ind=="fellow"]
like_schol_opponent<-data1$tc1lik_st[data1$school_treat_ind=="oppon"]
              
#Likelihood of selecting school by condition
comfort_schol_control<-data1$tc1comf2_st[data1$school_treat_ind=="control"]
comfort_schol_fellow<-data1$tc1comf2_st[data1$school_treat_ind=="fellow"]
comfort_schol_opponent<-data1$tc1comf2_st[data1$school_treat_ind=="oppon"]
              
#########
## The two t-tests for likelihood of selecting school
#control v. co-partisan
ts1<-t.test(like_schol_fellow,like_schol_control)
#control v. partisan opponent
ts2<-t.test(like_schol_opponent,like_schol_control)
## The two t-tests for comfort level with school
#control v. co-partisan
ts3<-t.test(comfort_schol_fellow,comfort_schol_control)
#control v. partisan opponent
ts4<-t.test(comfort_schol_opponent,comfort_schol_control)
              
              
#All the info from this t-test for ggplot2
school_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), 
                  up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                  .Names = c("param", "low2.5", "est", "up2.5"), 
                  row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means1   
              
              
school_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), 
                 up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                 .Names = c("param", "low2.5", "est", "up2.5"), 
                 row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means2
              
              
              
##############
df <- rbind(school_means1, school_means2)
      df$model <- rep(c('Likelihood of Selection', 'Comfort Level'), c(nrow(school_means1), nrow(school_means2)))
      df$param_f <- factor(df$param, 
      rev(c('Co-Partisans', "Opposing Partisans")),
      rev(c('Co-Partisans', 'Opposing Partisans')))
              
## Subfigure 1a
ggplot(df, aes(x = param_f, y = est)) + 
      geom_hline(yintercept = 0, color = "darkgray") + 
      geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
      geom_text(aes(label = est), nudge_x = 0.35) + 
      scale_x_discrete("") + 
      theme_bw() + 
      theme(text = element_text(size=14)) +
      ylab(NULL) + 
      ylim(-.15,.1)+
      coord_flip() +
      facet_grid(~model)+
      labs(x="",y="", title="School Experiment")+
      theme(plot.title = element_text(family = 'Helvetica', #plot title options
            color = 'black', 
            face = 'bold', 
            size = 14, 
            hjust = +.5))
              

#########################
# End school experiment
#########################
              
              
###
# Potential Co-worker Sally Experiment
###
              
# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$coworker_treat_ind[data1$tc2t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid3==1 | data1$tc2t==3 & data1$pid3==3]<-"fellow"
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid3==3 | data1$tc2t==3 & data1$pid3==1]<-"oppon"
              
# For t.tests for Co-Worker Experiment
#Likelihood of selecting a co-worker
likehire_coworker_control<-data1$tc2hire2_st[data1$coworker_treat_ind=="control"]
likehire_coworker_fellow<-data1$tc2hire2_st[data1$coworker_treat_ind=="fellow"]
likehire_coworker_opponent<-data1$tc2hire2_st[data1$coworker_treat_ind=="oppon"]
              
#Level of enjoyment enjoyment working with a co-worker
enjoy_coworker_control<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="control"]
enjoy_coworker_fellow<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="fellow"]
enjoy_coworker_opponent<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="oppon"]
              
#########
## The two t-tests for likelihood of selecting a co-worker
#control v. co-partisan
ts1<-t.test(likehire_coworker_fellow,likehire_coworker_control)
#control v. partisan opponent
ts2<-t.test(likehire_coworker_opponent,likehire_coworker_control)
## The two t-tests for level of enjoyment working with a co-worker
#control v. co-partisan
ts3<-t.test(enjoy_coworker_fellow,enjoy_coworker_control)
#control v. partisan opponent
ts4<-t.test(enjoy_coworker_opponent,enjoy_coworker_control)
              
              
#All the info from this t-test for ggplot2
coworker_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                   low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                   est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), 
                   up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                   .Names = c("param", "low2.5", "est", "up2.5"), 
                   row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means1
              

#For now just replicates what's above
coworker_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                   low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                   est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                   .Names = c("param", "low2.5", "est", "up2.5"), 
                   row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means2
              
              
              
##############
df <- rbind(coworker_means1, coworker_means2)
      df$model <- rep(c('Likelihood of Selection', 'Level of Enjoyment'), c(nrow(coworker_means1), nrow(coworker_means2)))
      df$param_f <- factor(df$param, 
      rev(c('Co-Partisans', "Opposing Partisans")),
      rev(c('Co-Partisans', 'Opposing Partisans')))
              
## Subfigure 1b
ggplot(df, aes(x = param_f, y = est)) + 
      geom_hline(yintercept = 0, color = "darkgray") + 
      geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
      geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
      scale_x_discrete("") + 
      theme_bw() + 
      theme(text = element_text(size=14)) +
      ylab(NULL) + 
      ylim(-.15,.1)+
      coord_flip() +
      facet_grid(~model)+
      labs(x="",y="", title="Co-worker Experiment")+
      theme(plot.title = element_text(family = 'Helvetica', #plot title options
                 color = 'black', 
                 face = 'bold', 
                 size = 14, 
                 hjust = +.5))
              

#########################
# End of Co-worker Experient
#########################
              
              
###
# New Product Experiment
###
              
# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$newproduct_treat_ind[data1$tc3t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid3==1 | data1$tc3t==3 & data1$pid3==3]<-"fellow"
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid3==3 | data1$tc3t==3 & data1$pid3==1]<-"oppon"
              
# For t.tests for New Soda Product
#Product interest
prodinter_control<-data1$tc3int2_st[data1$newproduct_treat_ind=="control"]
prodinter_fellow<-data1$tc3int2_st[data1$newproduct_treat_ind=="fellow"]
prodinter_opponent<-data1$tc3int2_st[data1$newproduct_treat_ind=="oppon"]
              
#Level of enjoyment
prodsucc_control<-data1$tc3suc2_st[data1$newproduct_treat_ind=="control"]
prodsucc_fellow<-data1$tc3suc2_st[data1$newproduct_treat_ind=="fellow"]
prodsucc_opponent<-data1$tc3suc2_st[data1$newproduct_treat_ind=="oppon"]
              
#########
## The two t-tests for product interest
#control v. co-partisan
ts1<-t.test(prodinter_fellow,prodinter_control)
#control v. partisan opponent
ts2<-t.test(prodinter_opponent,prodinter_control)
## The two t-tests for product success
#control v. co-partisan
ts3<-t.test(prodsucc_fellow,prodsucc_control)
#control v. partisan opponent
ts4<-t.test(prodsucc_opponent,prodsucc_control)
              
              
#All the info from this t-test for ggplot2
product_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)),
                  up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                  .Names = c("param", "low2.5", "est", "up2.5"), 
                  row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means1   
              
              
#For now just replicates what's above
product_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                  low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                  est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                  .Names = c("param", "low2.5", "est", "up2.5"), 
                  row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means2
              
              
              
##############
df <- rbind(product_means1, product_means2)
df$model <- rep(c('Product Interest', 'Product Success'), c(nrow(product_means1), nrow(product_means2)))
df$param_f <- factor(df$param, 
              rev(c('Co-Partisans', "Opposing Partisans")),
              rev(c('Co-Partisans', 'Opposing Partisans')))
              
## Subfigure 1c
ggplot(df, aes(x = param_f, y = est)) + 
                geom_hline(yintercept = 0, color = "darkgray") + 
                geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
                geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
                scale_x_discrete("") + 
                theme_bw() + 
                theme(text = element_text(size=14)) +
                ylab(NULL) + 
                ylim(-.15,.1)+
                coord_flip() +
                facet_grid(~model)+
                labs(x="",y="", title="New Soda Product Experiment")+
                theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                                color = 'black', 
                                                face = 'bold', 
                                                size = 14, 
                                                hjust = +.5))
              

#########################
# End New Product Experient
#########################
              
              
###
# Vacation Resort Experiment
###
              
# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$vacation_treat_ind[data1$tc4t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==1 | data1$tc4t==3 & data1$pid3==3]<-"fellow"
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==3 | data1$tc4t==3 & data1$pid3==1]<-"oppon"
              
# For t.tests vacation resort experiment
#Vacation Resort Interest
vacation_control<-data1$tc4int2_st[data1$vacation_treat_ind=="control"]
vacation_fellow<-data1$tc4int2_st[data1$vacation_treat_ind=="fellow"]
vacation_opponent<-data1$tc4int2_st[data1$vacation_treat_ind=="oppon"]
              
#########
## T-test for vacation resort interst
#control v. co-partisan
ts1<-t.test(vacation_fellow,vacation_control)
#control v. partisan opponent
ts2<-t.test(vacation_opponent,vacation_control)
              
#All the info from this t-test for ggplot2
vacation_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                   low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                   est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), 
                   up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                   .Names = c("param", "low2.5", "est", "up2.5"), 
                   row.names = 1:2, class = c("data.frame", "mplus.params"))
vacation_means1   
              
##############
df <- vacation_means1
df$model <- rep('Vacation Interest', nrow(vacation_means1) )
df$param_f <- factor(df$param, 
                rev(c('Co-Partisans', "Opposing Partisans")),
                rev(c('Co-Partisans', 'Opposing Partisans')))
              
## Subfigure 1d
ggplot(df, aes(x = param_f, y = est)) + 
                geom_hline(yintercept = 0, color = "darkgray") + 
                geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
                geom_text(aes(label = est), nudge_x = 0.35) + 
                scale_x_discrete("") + 
                theme_bw() + 
                theme(text = element_text(size=14)) +
                ylab(NULL) + 
                ylim(-.15,.1)+
                coord_flip() +
                facet_grid(~model)+ #
                labs(x="",y="", title="Vacation Resort")+
                theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                                color = 'black', 
                                                face = 'bold', 
                                                size = 14, 
                                                hjust = +.5))
 

#########################
# End Vacation Resort Experient
#########################
              
              











##################################################################################
### Figure 2 
### Begin figures for pure independents   ##############
##################################################################################
# Clearing working memory
rm(list=ls(all=TRUE))

data1<-read.csv("replicationdata_partisanship.csv")

#Subsetting data for pure indpendents only. There are 208 pure independents.
data1<-subset(data1, subset=(pid7==4))


###
# School Experiment
###

# treatment conditions 
# data1$tc1t==1 "control"
# data1$tc1t==2 "dem cue"
# data1$tc1t==3 "rep cue"


# For t.tests for school experiment
#Likelihood of selecting school by condition
like_schol_control<-data1$tc1lik_st[data1$tc1t==1]
like_schol_demcue<-data1$tc1lik_st[data1$tc1t==2]
like_schol_repcue<-data1$tc1lik_st[data1$tc1t==3]

#Likelihood of selecting school by condition
comfort_schol_control<-data1$tc1comf2_st[data1$tc1t==1]
comfort_schol_demcue<-data1$tc1comf2_st[data1$tc1t==2]
comfort_schol_repcue<-data1$tc1comf2_st[data1$tc1t==3]

#########
## The two t-tests for likelihood of selecting school
#control v. dem cue
ts1<-t.test(like_schol_demcue, like_schol_control)
#control v. rep cue
ts2<-t.test(like_schol_repcue, like_schol_control)
## The two t-tests for comfort level with school
#control v. dem cue
ts3<-t.test(comfort_schol_demcue, comfort_schol_control)
#control v. rep cue
ts4<-t.test(comfort_schol_repcue, comfort_schol_control)


#All the info from this t-test for ggplot2
school_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means1   


#For now just replicates what's above
school_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means2



##############
df <- rbind(school_means1, school_means2)
df$model <- rep(c('Likelihood of Selection', 'Comfort Level'), c(nrow(school_means1), nrow(school_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 2a
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE) ), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.23)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="School Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End school experiment
#########################



###
# Potential Co-worker Sally Experiment
###

# For t.tests for Co-Worker Experiment
#Likelihood of selecting a co-worker
likehire_coworker_control<-data1$tc2hire2_st[data1$tc1t==1]
likehire_coworker_demcue<-data1$tc2hire2_st[data1$tc1t==2]
likehire_coworker_repcue<-data1$tc2hire2_st[data1$tc1t==3]

#Level of enjoyment enjoyment working with a co-worker
enjoy_coworker_control<-data1$tc2enjoy2_st[data1$tc1t==1]
enjoy_coworker_demcue<-data1$tc2enjoy2_st[data1$tc1t==2]
enjoy_coworker_repcue<-data1$tc2enjoy2_st[data1$tc1t==3]


#########
## The two t-tests for likelihood of selecting school
#control v. dem cue
ts1<-t.test(likehire_coworker_demcue, likehire_coworker_control)
#control v. rep cue
ts2<-t.test(likehire_coworker_repcue, likehire_coworker_control)
## The two t-tests for comfort level with school
#control v. dem cue
ts3<-t.test(enjoy_coworker_demcue, enjoy_coworker_control)
#control v. rep cue
ts4<-t.test(enjoy_coworker_repcue, enjoy_coworker_control)



#All the info from this t-test for ggplot2
coworker_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means1   


#For now just replicates what's above
coworker_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                  est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means2



##############
df <- rbind(coworker_means1, coworker_means2)
df$model <- rep(c('Likelihood of Selection', 'Level of Enjoyment'), c(nrow(coworker_means1), nrow(coworker_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 2b
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.23)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="Co-worker Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End of Co-worker Experient
#########################


###
# New Product Experiment
###

# For t.tests for New Soda Product
#Product interest
prodinter_control<-data1$tc3int2_st[data1$tc1t==1]
prodinter_demcue<-data1$tc3int2_st[data1$tc1t==2]
prodinter_repcue<-data1$tc3int2_st[data1$tc1t==3]

#Level of enjoyment
prodsucc_control<-data1$tc3suc2_st[data1$tc1t==1]
prodsucc_demcue<-data1$tc3suc2_st[data1$tc1t==2]
prodsucc_repcue<-data1$tc3suc2_st[data1$tc1t==3]

#########
## The two t-tests for product interest
# dem cue v. control 
ts1<-t.test(prodinter_demcue,prodinter_control)
# rep cue v. control
ts2<-t.test(prodinter_repcue,prodinter_control)
## The two t-tests for product success
# dem cue v. control
ts3<-t.test(prodsucc_demcue,prodsucc_control)
# rep cue v. control
ts4<-t.test(prodsucc_repcue,prodsucc_control)


#All the info from this t-test for ggplot2
product_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means1   


#For now just replicates what's above
product_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means2



##############
df <- rbind(product_means1, product_means2)
df$model <- rep(c('Product Interest', 'Product Success'), c(nrow(product_means1), nrow(product_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.23)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="New Soda Product Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End New Product Experient
#########################


###
# Vacation Resort Experiment
###

# For t.tests vacation resort experiment
#Vacation Resort Interest
vacation_control<-data1$tc4int2_st[data1$tc1t==1]
vacation_demcue<-data1$tc4int2_st[data1$tc1t==2]
vacation_repcue<-data1$tc4int2_st[data1$tc1t==3]

#########
## T-test for vacation resort interst
# dem cue v. control
ts1<-t.test(vacation_demcue,vacation_control)
# rep cue v. control
ts2<-t.test(vacation_repcue,vacation_control)


#All the info from this t-test for ggplot2
vacation_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
vacation_means1   


##############
df <- vacation_means1
df$model <- rep('Vacation Interest', nrow(vacation_means1) )
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 1d
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.23)+
  coord_flip() +
  facet_grid(~model)+ #
  labs(x="",y="", title="Vacation Resort")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End Vacation Resort Experient
#########################











## Figure A2 in the Appendix
##################################
## Testing for effects of all independents (pures and leaners are all in one category).
## That is, how all independents as a group respond to different cues.
###################################

# Clearing working memory
rm(list=ls(all=TRUE))

data1<-read.csv("replicationdata_partisanship.csv")

#Subsetting data for pure indpendents only. There are 208 pure independents.
data1<-subset(data1, subset=(pid7==3 | pid7==4 | pid7==5))
sum(data1$pid7==4) ## 208 pure independents
sum(data1$pid7==3) ## 120 Democratic leaners 
sum(data1$pid7==5) ## 124 Republican leaners 
nrow(data1) ## 452 independents total

###
# School Experiment
###

# treatment conditions 
# data1$tc1t==1 "control"
# data1$tc1t==2 "dem cue"
# data1$tc1t==3 "rep cue"


# For t.tests for school experiment
#Likelihood of selecting school by condition
like_schol_control<-data1$tc1lik_st[data1$tc1t==1]
like_schol_demcue<-data1$tc1lik_st[data1$tc1t==2]
like_schol_repcue<-data1$tc1lik_st[data1$tc1t==3]

#Likelihood of selecting school by condition
comfort_schol_control<-data1$tc1comf2_st[data1$tc1t==1]
comfort_schol_demcue<-data1$tc1comf2_st[data1$tc1t==2]
comfort_schol_repcue<-data1$tc1comf2_st[data1$tc1t==3]

#########
## The two t-tests for likelihood of selecting school
#control v. dem cue
ts1<-t.test(like_schol_demcue, like_schol_control)
#control v. rep cue
ts2<-t.test(like_schol_repcue, like_schol_control)
## The two t-tests for comfort level with school
#control v. dem cue
ts3<-t.test(comfort_schol_demcue, comfort_schol_control)
#control v. rep cue
ts4<-t.test(comfort_schol_repcue, comfort_schol_control)


#All the info from this t-test for ggplot2
school_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means1   


#For now just replicates what's above
school_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means2



##############
df <- rbind(school_means1, school_means2)
df$model <- rep(c('Likelihood of Selection', 'Comfort Level'), c(nrow(school_means1), nrow(school_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 2a
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE) ), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.15)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="School Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End school experiment
#########################



###
# Potential Co-worker Sally Experiment
###

# For t.tests for Co-Worker Experiment
#Likelihood of selecting a co-worker
likehire_coworker_control<-data1$tc2hire2_st[data1$tc1t==1]
likehire_coworker_demcue<-data1$tc2hire2_st[data1$tc1t==2]
likehire_coworker_repcue<-data1$tc2hire2_st[data1$tc1t==3]

#Level of enjoyment enjoyment working with a co-worker
enjoy_coworker_control<-data1$tc2enjoy2_st[data1$tc1t==1]
enjoy_coworker_demcue<-data1$tc2enjoy2_st[data1$tc1t==2]
enjoy_coworker_repcue<-data1$tc2enjoy2_st[data1$tc1t==3]


#########
## The two t-tests for likelihood of selecting school
#control v. dem cue
ts1<-t.test(likehire_coworker_demcue, likehire_coworker_control)
#control v. rep cue
ts2<-t.test(likehire_coworker_repcue, likehire_coworker_control)
## The two t-tests for comfort level with school
#control v. dem cue
ts3<-t.test(enjoy_coworker_demcue, enjoy_coworker_control)
#control v. rep cue
ts4<-t.test(enjoy_coworker_repcue, enjoy_coworker_control)



#All the info from this t-test for ggplot2
coworker_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means1   


#For now just replicates what's above
coworker_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                  est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means2



##############
df <- rbind(coworker_means1, coworker_means2)
df$model <- rep(c('Likelihood of Selection', 'Level of Enjoyment'), c(nrow(coworker_means1), nrow(coworker_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 2b
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.15)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="Co-worker Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End of Co-worker Experient
#########################


###
# New Product Experiment
###

# For t.tests for New Soda Product
#Product interest
prodinter_control<-data1$tc3int2_st[data1$tc1t==1]
prodinter_demcue<-data1$tc3int2_st[data1$tc1t==2]
prodinter_repcue<-data1$tc3int2_st[data1$tc1t==3]

#Level of enjoyment
prodsucc_control<-data1$tc3suc2_st[data1$tc1t==1]
prodsucc_demcue<-data1$tc3suc2_st[data1$tc1t==2]
prodsucc_repcue<-data1$tc3suc2_st[data1$tc1t==3]

#########
## The two t-tests for product interest
# dem cue v. control 
ts1<-t.test(prodinter_demcue,prodinter_control)
# rep cue v. control
ts2<-t.test(prodinter_repcue,prodinter_control)
## The two t-tests for product success
# dem cue v. control
ts3<-t.test(prodsucc_demcue,prodsucc_control)
# rep cue v. control
ts4<-t.test(prodsucc_repcue,prodsucc_control)


#All the info from this t-test for ggplot2
product_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means1   


#For now just replicates what's above
product_means2 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means2



##############
df <- rbind(product_means1, product_means2)
df$model <- rep(c('Product Interest', 'Product Success'), c(nrow(product_means1), nrow(product_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE) ), nudge_x = 0.35) +   scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.15)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="New Soda Product Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End New Product Experient
#########################


###
# Vacation Resort Experiment
###

# For t.tests vacation resort experiment
#Vacation Resort Interest
vacation_control<-data1$tc4int2_st[data1$tc1t==1]
vacation_demcue<-data1$tc4int2_st[data1$tc1t==2]
vacation_repcue<-data1$tc4int2_st[data1$tc1t==3]

#########
## T-test for vacation resort interst
# dem cue v. control
ts1<-t.test(vacation_demcue,vacation_control)
# rep cue v. control
ts2<-t.test(vacation_repcue,vacation_control)


#All the info from this t-test for ggplot2
vacation_means1 <- structure(list(param = c("Democratic Cue", "Republican Cue"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
vacation_means1   


##############
df <- vacation_means1
df$model <- rep('Vacation Interest', nrow(vacation_means1) )
df$param_f <- factor(df$param, 
                     rev(c('Democratic Cue', "Republican Cue")),
                     rev(c('Democratic Cue', 'Republican Cue')))

## Subfigure 1d
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.15,.15)+
  coord_flip() +
  facet_grid(~model)+ #
  labs(x="",y="", title="Vacation Resort")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End Vacation Resort Experient
#########################



## Figure A3 in the Appendix
#############################################################################
###############################
## Looking at results for leaners only
################################
rm(list=ls(all=TRUE))

data1<-read.csv("replicationdata_partisanship.csv")
data1<-subset(data1, subset=(pid7==3 | pid7==5)) ## subsetting data just for independents. (no pure independents)

###
# School Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$school_treat_ind[data1$tc1t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$school_treat_ind[data1$tc1t==2 & data1$pid7==3 | data1$tc1t==3 & data1$pid7==5]<-"fellow"
data1$school_treat_ind[data1$tc1t==2 & data1$pid7==5 | data1$tc1t==3 & data1$pid7==3]<-"oppon"

# For t.tests for school experiment
#Likelihood of selecting school by condition
like_schol_control<-data1$tc1lik_st[data1$school_treat_ind=="control"]
like_schol_fellow<-data1$tc1lik_st[data1$school_treat_ind=="fellow"]
like_schol_opponent<-data1$tc1lik_st[data1$school_treat_ind=="oppon"]

#Likelihood of selecting school by condition
comfort_schol_control<-data1$tc1comf2_st[data1$school_treat_ind=="control"]
comfort_schol_fellow<-data1$tc1comf2_st[data1$school_treat_ind=="fellow"]
comfort_schol_opponent<-data1$tc1comf2_st[data1$school_treat_ind=="oppon"]

#########
## The two t-tests for likelihood of selecting school
#control v. co-partisan
ts1<-t.test(like_schol_fellow,like_schol_control)
#control v. partisan opponent
ts2<-t.test(like_schol_opponent,like_schol_control)
## The two t-tests for comfort level with school
#control v. co-partisan
ts3<-t.test(comfort_schol_fellow,comfort_schol_control)
#control v. partisan opponent
ts4<-t.test(comfort_schol_opponent,comfort_schol_control)


#All the info from this t-test for ggplot2
school_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means1   


school_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                           .Names = c("param", "low2.5", "est", "up2.5"), 
                           row.names = 1:2, class = c("data.frame", "mplus.params"))
school_means2



##############
df <- rbind(school_means1, school_means2)
df$model <- rep(c('Likelihood of Selection', 'Comfort Level'), c(nrow(school_means1), nrow(school_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1a
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.17,.18)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="School Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End school experiment
#########################


###
# Potential Co-worker Sally Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$coworker_treat_ind[data1$tc2t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid7==3 | data1$tc2t==3 & data1$pid7==5]<-"fellow"
data1$coworker_treat_ind[data1$tc2t==2 & data1$pid7==5 | data1$tc2t==3 & data1$pid7==3]<-"oppon"

# For t.tests for Co-Worker Experiment
#Likelihood of selecting a co-worker
likehire_coworker_control<-data1$tc2hire2_st[data1$coworker_treat_ind=="control"]
likehire_coworker_fellow<-data1$tc2hire2_st[data1$coworker_treat_ind=="fellow"]
likehire_coworker_opponent<-data1$tc2hire2_st[data1$coworker_treat_ind=="oppon"]

#Level of enjoyment enjoyment working with a co-worker
enjoy_coworker_control<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="control"]
enjoy_coworker_fellow<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="fellow"]
enjoy_coworker_opponent<-data1$tc2enjoy2_st[data1$coworker_treat_ind=="oppon"]

#########
## The two t-tests for likelihood of selecting a co-worker
#control v. co-partisan
ts1<-t.test(likehire_coworker_fellow,likehire_coworker_control)
#control v. partisan opponent
ts2<-t.test(likehire_coworker_opponent,likehire_coworker_control)
## The two t-tests for level of enjoyment working with a co-worker
#control v. co-partisan
ts3<-t.test(enjoy_coworker_fellow,enjoy_coworker_control)
#control v. partisan opponent
ts4<-t.test(enjoy_coworker_opponent,enjoy_coworker_control)


#All the info from this t-test for ggplot2
coworker_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means1

#For now just replicates what's above
coworker_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                  low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                  est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
coworker_means2



##############
df <- rbind(coworker_means1, coworker_means2)
df$model <- rep(c('Likelihood of Selection', 'Level of Enjoyment'), c(nrow(coworker_means1), nrow(coworker_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1b
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.17,.18)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="Co-worker Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End of Co-worker Experient
#########################


###
# New Product Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$newproduct_treat_ind[data1$tc3t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid7==3 | data1$tc3t==3 & data1$pid7==5]<-"fellow"
data1$newproduct_treat_ind[data1$tc3t==2 & data1$pid7==5 | data1$tc3t==3 & data1$pid7==3]<-"oppon"

# For t.tests for New Soda Product
#Product interest
prodinter_control<-data1$tc3int2_st[data1$newproduct_treat_ind=="control"]
prodinter_fellow<-data1$tc3int2_st[data1$newproduct_treat_ind=="fellow"]
prodinter_opponent<-data1$tc3int2_st[data1$newproduct_treat_ind=="oppon"]

#Level of enjoyment
prodsucc_control<-data1$tc3suc2_st[data1$newproduct_treat_ind=="control"]
prodsucc_fellow<-data1$tc3suc2_st[data1$newproduct_treat_ind=="fellow"]
prodsucc_opponent<-data1$tc3suc2_st[data1$newproduct_treat_ind=="oppon"]

#########
## The two t-tests for product interest
#control v. co-partisan
ts1<-t.test(prodinter_fellow,prodinter_control)
#control v. partisan opponent
ts2<-t.test(prodinter_opponent,prodinter_control)
## The two t-tests for product success
#control v. co-partisan
ts3<-t.test(prodsucc_fellow,prodsucc_control)
#control v. partisan opponent
ts4<-t.test(prodsucc_opponent,prodsucc_control)


#All the info from this t-test for ggplot2
product_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                 low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                 est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), 
                                 up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means1   


#For now just replicates what's above
product_means2 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                 low2.5 = c(ts3$conf.int[1], ts4$conf.int[1]), 
                                 est = c(round(ts3$estimate[1]-ts3$estimate[2],2), round(ts4$estimate[1]-ts4$estimate[2],2)), 
                                 up2.5 = c(ts3$conf.int[2], ts4$conf.int[2])), 
                            .Names = c("param", "low2.5", "est", "up2.5"), 
                            row.names = 1:2, class = c("data.frame", "mplus.params"))
product_means2



##############
df <- rbind(product_means1, product_means2)
df$model <- rep(c('Product Interest', 'Product Success'), c(nrow(product_means1), nrow(product_means2)))
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1c

ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = format(est, drop0trailing = FALSE)), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.17,.18)+
  coord_flip() +
  facet_grid(~model)+
  labs(x="",y="", title="New Soda Product Experiment")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End New Product Experient
#########################


###
# Vacation Resort Experiment
###

# treatment conditions including independents in partisan categories
#will have to drop pid3==2 "pure independents"
data1$vacation_treat_ind[data1$tc4t==1]<-"control" #this is viewing control group candidate 
#or partisan candidate when you're a pur independent
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==1 | data1$tc4t==3 & data1$pid3==3]<-"fellow"
data1$vacation_treat_ind[data1$tc4t==2 & data1$pid3==3 | data1$tc4t==3 & data1$pid3==1]<-"oppon"

# For t.tests vacation resort experiment
#Vacation Resort Interest
vacation_control<-data1$tc4int2_st[data1$vacation_treat_ind=="control"]
vacation_fellow<-data1$tc4int2_st[data1$vacation_treat_ind=="fellow"]
vacation_opponent<-data1$tc4int2_st[data1$vacation_treat_ind=="oppon"]

#########
## T-test for vacation resort interst
#control v. co-partisan
ts1<-t.test(vacation_fellow,vacation_control)
#control v. partisan opponent
ts2<-t.test(vacation_opponent,vacation_control)

#All the info from this t-test for ggplot2
vacation_means1 <- structure(list(param = c("Co-Partisans", "Opposing Partisans"), 
                                  low2.5 = c(ts1$conf.int[1], ts2$conf.int[1]), 
                                  est = c(round(ts1$estimate[1]-ts1$estimate[2],2), round(ts2$estimate[1]-ts2$estimate[2],2)), up2.5 = c(ts1$conf.int[2], ts2$conf.int[2])), 
                             .Names = c("param", "low2.5", "est", "up2.5"), 
                             row.names = 1:2, class = c("data.frame", "mplus.params"))
vacation_means1   

##############
df <- vacation_means1
df$model <- rep('Vacation Interest', nrow(vacation_means1) )
df$param_f <- factor(df$param, 
                     rev(c('Co-Partisans', "Opposing Partisans")),
                     rev(c('Co-Partisans', 'Opposing Partisans')))

## Subfigure 1d
ggplot(df, aes(x = param_f, y = est)) + 
  geom_hline(yintercept = 0, color = "darkgray") + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "black") +
  geom_text(aes(label = est), nudge_x = 0.35) + 
  scale_x_discrete("") + 
  theme_bw() + 
  theme(text = element_text(size=14)) +
  ylab(NULL) + 
  ylim(-.17,.18)+
  coord_flip() +
  facet_grid(~model)+ #
  labs(x="",y="", title="Vacation Resort")+
  theme(plot.title = element_text(family = 'Helvetica', #plot title options
                                  color = 'black', 
                                  face = 'bold', 
                                  size = 14, 
                                  hjust = +.5))


#########################
# End Vacation Resort Experient
#########################










## Table 2
#################################### 
#####  Testing for effect sizes
##### 
# Clearing working memory
rm(list=ls(all=TRUE))

data1<-read.csv("replication_long_data.csv")

library(lme4) ## package for mixed models
library(lmerTest) ## package to get p-values in model output
library(stargazer) ## dope tex tables

data1<-subset(data1, subset=(pid7!=4)) ## Dropping pure independents, becasue they were omited from main analysis 

## Here both experiment ("outcome") and person (idvar) are random effects
model1 <- lmer(evaluation ~ person + factor(treatment)+factor(treatment):person+(1|outcome)+(1|idvar), data=data1)
summary(model1)

# One tailed p-values for the interaction terms
inp <- 0.6198
inp / 2
1 - (inp / 2)

outp <- 0.7500
outp / 2
1 - (outp / 2)

# 90% confidence intervals around coefficients
confint(model1, c("person:factor(treatment)2","person:factor(treatment)3"), level = 0.90)



class(model1) <- "lmerMod"
stargazer(model1, star.cutoffs=(0.05))



